﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.SessionState;
using JMcms.Web.UI;
using JMcms.Common;
using JMcms.Web.Plugin.Mailbox.Model;

namespace JMcms.Web.Plugin.Mailbox
{
    /// <summary>
    /// 管理后台AJAX处理页
    /// </summary>
    public class ajax : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = JMRequest.GetQueryString("action");

            switch (action)
            {
                case "add": //发布留言
                    mailbox_add(context);
                    break;
            }

        }
        #region
        private mailbox_attach get_attach(System.IO.Stream theStream, string fileName, bool isThumbnail, bool isWater,string _mailbox_id)
        {
            UpLoad upload = new UpLoad();
            string msg = upload.FileSaveAs(FileHelper.ConvertStreamToByteBuffer(theStream), fileName, false, false);
            Dictionary<string, object> dic = JsonHelper.DataRowFromJSON(msg);

            mailbox_attach a = new Model.mailbox_attach { mailbox_id = _mailbox_id, file_name = dic["name"].ToString(), file_path = dic["path"].ToString(), file_size = Utils.StrToInt(dic["size"].ToString(), 0), file_ext = dic["ext"].ToString() };
            return a;
        }
        #endregion
        #region 发布留言================================
        private void mailbox_add(HttpContext context)
        {
            StringBuilder strTxt = new StringBuilder();
            BLL.mailbox bll = new BLL.mailbox();
            Model.mailbox model = new Model.mailbox();
            List<mailbox_attach> list = new List<Model.mailbox_attach>();
            int _site_id = JMRequest.GetQueryInt("site");
            int _mailtype = JMRequest.GetQueryInt("mailtype");
            int _is_open =Convert.ToInt32(JMRequest.GetFormString("IsOpen"));
            string _code = JMRequest.GetFormString("txtCode");
            string _title = JMRequest.GetFormString("txtTitle");
            string _content = JMRequest.GetFormString("txtContent");
            string _user_name = JMRequest.GetFormString("txtUserName");
            string _user_tel = JMRequest.GetFormString("txtUserTel");
            string _user_qq = JMRequest.GetFormString("txtUserQQ");
            string _user_email = JMRequest.GetFormString("txtUserEmail");
            string _user_card= JMRequest.GetFormString("txtUserCard");
            //string _apply_attach= JMRequest.GetFormString("txtApplyAttach");
            HttpPostedFile _apply_attach = context.Request.Files["txtApplyAttach"];
            if (_apply_attach != null)
            {
                if (_apply_attach.ContentLength != 0)
                {
                    list.Add(get_attach(_apply_attach.InputStream, _apply_attach.FileName, false, false, model.apply_attach));
                }

            }

            string _card_attach =JMRequest.GetFormString("txtCardAttach");
            string _business_attach= JMRequest.GetFormString("txtBusinessAttach");
            string _company_name= JMRequest.GetFormString("txtCompanyName");
            string _company_code= JMRequest.GetFormString("txtCompanyCode");
            string _credit_code= JMRequest.GetFormString("txtCreditCode");
            
            //检查所属站点
            if (_site_id == 0)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，站点传输参数有误！\"}");
                return;
            }
      

            //校检验证码
            if (string.IsNullOrEmpty(_code))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入验证码！\"}");
                return;
            }
            if (context.Session[JMKeys.SESSION_CODE] == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，验证码已过期！\"}");
                return;
            }
            if (_code.ToLower() != (context.Session[JMKeys.SESSION_CODE].ToString()).ToLower())
            {
                context.Response.Write("{\"status\":0, \"msg\":\"验证码与系统的不一致！\"}");
                return;
            }
            //校检用户名
            if (string.IsNullOrEmpty(_user_name))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入申请人姓名！\"}");
                return;
            }
            if (string.IsNullOrEmpty(_content))
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"对不起，请输入留言的内容！\"}");
                return;
            }
            //校检联系方式
            if (string.IsNullOrEmpty(_user_tel))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入申请人联系方式！\"}");
                return;
            }
            #region 校验
            //检查信件类型
            if (_mailtype == 0)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"对不起，类型传输参数有误！\"}");
                return;
            }//意见建议
            else if (_mailtype == 1)
            {
                //校检标题
                if (string.IsNullOrEmpty(_title))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入意见 标题！\"}");
                    return;
                }
            }//异议申请
            else if (_mailtype == 7)
            {
                //校检标题
                if (string.IsNullOrEmpty(_title))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入异议主题！\"}");
                    return;
                }
                //校检身份证号码_user_card _apply_attach
                if (string.IsNullOrEmpty(_user_card))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入身份证号码！\"}");
                    return;
                }
                //校检证明材料_user_card _apply_attach
                //if (string.IsNullOrEmpty(_apply_attach))
                //{
                //    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请上传证明材料！\"}");
                //    return;
                //}
            }//核查报告申请  
            else if (_mailtype == 3)
            {
                //校检证明材料
                if (string.IsNullOrEmpty(_company_name))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入企业名称！\"}");
                    return;
                }
                //校检统一社会信用代码
                if (string.IsNullOrEmpty(_credit_code))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入统一社会信用代码！\"}");
                    return;
                }
                //校检组织机构代码
                if (string.IsNullOrEmpty(_company_code))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请输入企业组织机构代码！\"}");
                    return;
                }
                //校检营业执照
                if (string.IsNullOrEmpty(_business_attach))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请上传营业执照！\"}");
                    return;
                }
                //校检身份证复印件
                if (string.IsNullOrEmpty(_card_attach))
                {
                    context.Response.Write("{\"status\":0, \"msg\":\"对不起，请上传身份证复印件！\"}");
                    return;
                }
            }
            #endregion
            model.site_id = _site_id;
            model.is_open = _is_open;
            model.title = Utils.DropHTML(_title);
            model.content = Utils.ToHtml(_content);
            model.user_name = Utils.DropHTML(_user_name);
            model.user_tel = Utils.DropHTML(_user_tel);
            model.user_qq = Utils.DropHTML(_user_qq);
            model.user_email = Utils.DropHTML(_user_email);
            model.add_time = DateTime.Now;
            model.is_lock = 1; //不需要审核，请改为0
            model.user_card= Utils.DropHTML(_user_card);
           // model.apply_attach = _apply_attach;
            model.card_attach = _card_attach;
            model.business_attach = _business_attach;
            model.company_name = Utils.DropHTML(_company_name);
            model.company_code = Utils.DropHTML(_company_code);
            model.credit_code = Utils.DropHTML(_credit_code);
            model.mail_type = _mailtype;
       
            if (bll.Add(model) > 0)
            {
                //附件上传
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        bll.Add_Attach(list[i]);
                    }
                }
                context.Response.Write("{\"status\": 1, \"msg\": \"恭喜您，留言提交成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"对不起，保存过程中发生错误！\"}");
            return;
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}